<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script src="/javascript/jquery.alerts.js" type="text/javascript"></script>
<link href="/stylesheets/jquery.alerts.css" rel="stylesheet" type="text/css" media="screen" />

<script type="text/javascript">
		// Number of options
		var numOfOptions=5; 
		// JQuery Code
		$(document).ready(function() {
			$('#compform').submit(function() {
				res=ValidateForm()
				if (res==0) {
					jAlert("Title can not be empty and must be less than {{ maxTitleLength }} chars long and in English/numbers", "Attention");
					return false;
				}
				else if (res==-1) {
					jAlert("Please insert valid entry amount (whole number only)", "Attention");
					return false;
				}
				else if (res==-2) {
					jAlert("Please insert at least 2 options, and select one of them as your bet", "Attention");
					return false;
				}
				else if (res==-3) {
					jAlert("Each entity can contain up to 3 stock names only", "Attention");
					return false;
				}
				else if (res==-4) {
					jAlert("Please use English letters and numbers only", "Attention");
					return false;
				}
				else if (res==-5) {
					jAlert("Stock name must not exceed 50 characters", "Attention");
					return false;
				}
					
				else return true;
			});
			return false;
		});

		function ValidateForm() {

			if ($('#title').val()=='' || $('#title').val().search(/[^a-zA-Z0-9 ]/g) != -1 || $('#title').val().length>{{ maxTitleLength }}) return 0;
			else if ($('#amount').val()=='' || $('#amount').val()!=parseInt($('#amount').val())) return -1;
			// Check if there are at least two options
			flagOptions=0;
			// Check if user chose an option
			flagChosen=0;
			for (var i=0;i<numOfOptions;i++) {
				if ($('#op' + i).val()!='') {
					entities=$('#op' + i).val().split(",");
					if (entities.length>3) return -3;
					for (var j=0;j<entities.length;j++) {
						var res=ValidateStock(entities[j]);
						if (res!=1) return res;
					}
					flagOptions++;
					{% if not advertise %} if (document.getElementById("rad" + i).checked) flagChosen=1; {% endif %}
				}
			}
			if (flagOptions<2{% if not advertise %} || flagChosen==0{% endif %}) return -2;
			return 1;
		}

		function ValidateStock(stockName) {
			if (stockName.search(/[^a-zA-Z0-9 ]/g) != -1 || stockName=='') return -4;
			else if (stockName.length>{{ maxStockLength }}) return -5;
			return 1;
		}



</script> 